**********************************************************************************************
************** IMPORT VOTING ARCHIVE  ********************************************************
**********************************************************************************************
use dta\EldreVoteringerPerson, clear
merge lpnr using dta\EldreVoteringerSak.dta
drop if _merge==2
drop _merge

**********************************************************************************************
******************* DATA PREPERATION  ********************************************************
**********************************************************************************************

rename aar year
drop if year<1907|year>1940

rename person pid_NSD
replace pid_NSD=. if pid_NSD==99999
replace pid_NSD=. if pid_NSD==0

gen electionperiod=.
replace electionperiod=1 if year>1906 & year<1910
replace electionperiod=2 if year>1909 & year<1913
replace electionperiod=3 if year>1912 & year<1916
replace electionperiod=4 if year>1915 & year<1919
replace electionperiod=5 if year>1918 & year<1922
replace electionperiod=6 if year>1921 & year<1925
replace electionperiod=7 if year>1924 & year<1928
replace electionperiod=8 if year>1927 & year<1931
replace electionperiod=9 if year>1930 & year<1934
replace electionperiod=10 if year>1933 & year<1937
replace electionperiod=11 if year>1936 & year<1940

rename year votingyear

******************************************************************************************************
*********************** GEN NEW VOTING NUMBER ********************************************************
******************************************************************************************************
foreach sal in 1 2 3{
preserve
keep if sal==`sal'
egen votnr_max=max(votnr), by(votingyear)

	forvalues i = 1907(1)1936 {
	gen votnr_max_`i'=.
	replace votnr_max_`i'=votnr_max if votingyear==`i'
	egen votnr_max_`i'_=max(votnr_max_`i')
	drop votnr_max_`i'
	}

sum votnr votnr_max votnr_max_1918_ if votingyear==1918

gen votnr_new=votnr if votingyear==1907
replace votnr_new=votnr+votnr_max_1907 if votingyear==1908
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908 if votingyear==1909
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909 if votingyear==1910
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910 if votingyear==1911
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911 if votingyear==1912
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912 if votingyear==1913
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913 if votingyear==1914
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914 if votingyear==1915
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914+votnr_max_1915 if votingyear==1916
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914+votnr_max_1915+votnr_max_1916 if votingyear==1917
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914+votnr_max_1915+votnr_max_1916+votnr_max_1917 if votingyear==1918
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914+votnr_max_1915+votnr_max_1916+votnr_max_1917+votnr_max_1918 if votingyear==1919
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914+votnr_max_1915+votnr_max_1916+votnr_max_1917+votnr_max_1918+votnr_max_1919 if votingyear==1920
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914+votnr_max_1915+votnr_max_1916+votnr_max_1917+votnr_max_1918+votnr_max_1919+votnr_max_1920 if votingyear==1921
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914+votnr_max_1915+votnr_max_1916+votnr_max_1917+votnr_max_1918+votnr_max_1919+votnr_max_1920+votnr_max_1921 if votingyear==1922
replace votnr_new=votnr+votnr_max_1907+votnr_max_1908+votnr_max_1909+votnr_max_1910+votnr_max_1911+votnr_max_1912+votnr_max_1913+votnr_max_1914+votnr_max_1915+votnr_max_1916+votnr_max_1917+votnr_max_1918+votnr_max_1919+votnr_max_1920+votnr_max_1921+votnr_max_1922 if votingyear==1923

keep lpnr votnr_new
sort lpnr
save dta\GenerateVotenumber_`sal'_.dta, replace
restore
}

forvalues i = 1(1)3{
sort lpnr
merge lpnr using dta\GenerateVotenumber_`i'_, update
gen _merge`i'=_merge
drop _merge
}

replace votnr_new= votnr_new-960 if sal==1
replace votnr_new= votnr_new-555 if sal==2
replace votnr_new= votnr_new-219 if sal==3

******************************************************************************************************erase


**** people in dta\DataSet_Elected1906_1936 for electionyear==1918, but not in voteringsarkivet
/*
        +--------------------------------+
        | pid_NSD       candidatename_ed |
        |--------------------------------|
349336. |   10033        Halvdan Aarsrud |    
349343. |   10068        Ivar Aavatsmark |
349359. |   10233   Carl Severin Bentzen |
349464. |   11457    Gunder Anton Jahren |
349492. |   11632   Aanon Gunnar Knudsen |
        |--------------------------------|
349533. |   12025      Ole Monsen Mjelde |
        +--------------------------------+

		*/
		
replace pid_NSD=10033 if pid_NSD==10032 & votingyear>1918 & votingyear<1924   /* 10032 Hans Aarsrud first enter in 1931 */
replace pid_NSD=10068 if pid_NSD==10069 & votingyear>1918 & votingyear<1924   /* 10069 Ole Severin Aavatsmark is in Storting 1906 and 1915, but not in 1918 */
replace pid_NSD=10697 if pid_NSD==10695 & votingyear>1918 & votingyear<1924   /* 10697 Fredrik Enge is in Storting 1915, but not in 1918 */
replace pid_NSD=. if pid_NSD==11458 & votingyear>1920  /* 11458 Kristian Jahren is in Storting 1915, but not in 1918, should be gunder anton jahren, but NSD lists Kristian Jahren with two votes in 1920, hence missing */
replace pid_NSD=11457 if pid_NSD==11458 & votingyear>1918 & votingyear<1924  /* 11458 Kristian Jahren is in Storting 1915, but not in 1918, should be gunder anton jahren */
replace pid_NSD=10118  if pid_NSD==10119  & votingyear>1918 & votingyear<1924   /* 10119 ole andreas syrenius anderssen in 1800s! , should be 10118 ole anton anderssen deputy */
replace pid_NSD=12118  if pid_NSD==12113  & votingyear>1918 & votingyear<1924   /* MØLLER 1800S , SHOULD BE 12118 MØLLER DEPUTY */
replace pid_NSD=10206  if pid_NSD==10205  & votingyear>1918 & votingyear<1924   /* Hans Bang in 1800s, should be 10206 joakim sveder bang */
replace pid_NSD=11632  if pid_NSD==11635  & votingyear>1918 & votingyear<1924   /* CHRISTIAN HOLTERMANN KNUDSEN 1906 1909 1912 , should be Aanon Gunnar Knudsen */
replace pid_NSD=11065  if pid_NSD==11064  & votingyear>1918 & votingyear<1924   /* HALS 1889 , should probably be 11065 Halvorsen deputy */

replace pid_NSD=13138 if pid_NSD==130150  
replace pid_NSD=13150 if pid_NSD==131138  
replace pid_NSD=13100 if pid_NSD==130100  

/* post-reform */
replace pid_NSD=10338 if pid_NSD==10335  & votingyear>1918 & votingyear<1924   /*     /* Blackstad 1883, should be Blakstad 10338*/   */
replace pid_NSD=11098  if pid_NSD==11097   & votingyear>1918 & votingyear<1924   /*      Hans Thorvald Hansen (MP 1909) --> 11098 Haakon Thorvald Hansen    */
replace pid_NSD=12332  if pid_NSD==12333   & votingyear>1918 & votingyear<1924    /* levor Pedersen, not in storting , should be 12332 lars Pedersen    */
replace pid_NSD=12747  if pid_NSD==12748   & votingyear>1918 & votingyear<1924   /* different emil stang, should be 12747 */
replace pid_NSD=12086 if pid_NSD==12868  & votingyear>1918 & votingyear<1924      /* peder svendsen (1900), should be 12086 Otto Thott Fritzner Muller    */



preserve
**********************************************************************************************
******************************************** PREPARE FILE FOR FIGURE A.2-A.3******************
**********************************************************************************************

**********************************************************************************************
************** MERGE IN FILE WITH CANDIDATE AND DISTRICT CHARACTERISTICS *********************
*********************************************************************************************

sort pid_NSD electionperiod
merge pid_NSD electionperiod using dta\DataSet_Elected1906_1936

drop if vot=="0"
tab candidatename_ed if _merge==2 & electionyear==1918
tab candidatename_ed if _merge==2 & electionyear==1921

drop if _merge==2 /* only in DataSet, cannot find match in voting archive */

drop if votingyear>1923 
drop if votingyear<1919
tab _merge

*** number of unique votes
*preserve
*gen count=1
*collapse count sal, by(lpnr)
*li
*li if sal!=3
*restore

drop if sal==3  /* DROP LAGTING */
sum

egen min_votnr_new=min(votnr_new), by(pid_NSD)  /* first vote MP is casting */
egen max_votnr_new=max(votnr_new), by(pid_NSD)  /* last vote MP is casting */
*tab candidatename_ed if max_votnr==-1  /* candidates exiting before PR */
*tab candidatename_ed if min_votnr==0  /* candidates entering with PR */

tab _merge
drop if _merge==1  /* only keep observations where we identify candidate, the remaing are primarily deputies */

***************************************************************************************************************
*********** for each vote (identified by votnr) find voting cohesion by party**********************************
***************************************************************************************************************

** Votering (VOT)
*0=Møtte ikke, permitert
*1=Stemte i flertallet
*2=Stemte i mindretallet
*3=Var fraværende i voteringen
*NULL= missing

gen vote=0
replace vote=1 if vot=="1"
replace vote=2 if vot=="2"
replace vote=3 if vot=="3"  /* updated */

***************************************************************************************************************
*********** Identifying cabinets ******************************************************************************
***************************************************************************************************************

gen LibCabinet=0
replace LibCabinet=1 if votnr_new<-96.5 & sal==1  /* Knudsen cabinet */
replace LibCabinet=1 if votnr_new>-29.5 & votnr_new<50.5 & sal==1  /* Blehr cabinet */

replace LibCabinet=1 if votnr_new<-77.5 & sal==2 /* Knudsen cabinet */
replace LibCabinet=1 if votnr_new>-18.5 & votnr_new<15.5 & sal==2  /* Blehr cabinet */

gen missing=0
replace missing=1 if vote==0
replace missing=1 if vote==3
tab missing /* We also exclude absentees (7,631 observations) */

gen partybloc="."
replace partybloc="Labor" if party=="s"|party=="dna"|party=="nsa" 
replace partybloc="Conservative" if party=="h"|party=="h_fv"|party=="fv"
replace partybloc="Liberal" if party=="v"|party=="v_a"|party=="a"|party=="rf"
tab party if partybloc=="."
tab partyname if partybloc=="."

egen partybloc_missing=mean(missing), by(lpnr partybloc)
*hist partybloc_missing

egen case_missing=mean(missing), by(lpnr)
*hist partybloc_missing

drop if missing==1 /* MP permitert */
tab party if partybloc=="."  
keep if partybloc!="."  /* independent and farmers party */

*******************************************************************************
*******************************************************************************
*******************************************************************************
gen count=1
gen count_with_majority=0
replace count_with_majority=1 if vote==1
replace count_with_majority=. if vote==3 /* MP abstained */
egen partybloc_with_majority=mean(count_with_majority), by(lpnr partybloc)
egen overall_with_majority=mean(count_with_majority), by(lpnr)

gen deviation=0
replace deviation=1 if partybloc_with_majority<0.5 & vote==1 /*candidate voting with majority, partybloc not voting with majority */
replace deviation=1 if partybloc_with_majority>0.5 & vote==2 /*candidate not voting with majority, partybloc voting with majority */
*******replace deviation=1 if vote==3 /*candidate abstaining --- updated */


************************************************************************************************
************************************************************************************************
************************************************************************************************

replace tekst=lower(tekst)
gen tax=0
replace tax=1 if strpos(tekst,"skatt")
replace tax=1 if strpos(tekst,"skat")
replace tax=1 if strpos(tekst,"avgift")
replace tax=1 if strpos(tekst,"afgift")
*replace tax=1 if strpos(tekst,"tariff")
*replace tax=1 if strpos(tekst,"told")

*gen spending=0
*replace spending=1 if strpos(tekst,"bev.")
*replace spending=1 if strpos(tekst,"bevilgning")

gen agriculture=0
replace agriculture=1 if strpos(tekst,"fisk")
replace agriculture=1 if strpos(tekst,"landbruk")
replace agriculture=1 if strpos(tekst,"landbrug")

gen railway=0
replace railway=1 if strpos(tekst,"jernbane")
replace railway=1 if strpos(tekst,"banen")

gen military=0
replace military=1 if strpos(tekst,"hæren")
replace military=1 if strpos(tekst,"marin")
replace military=1 if strpos(tekst,"vernepligtige")
replace military=1 if strpos(tekst,"militær")

gen school=0
replace school=1 if strpos(tekst,"skole")

gen alcohol=0
replace alcohol=1 if strpos(tekst,"brændevin")
replace alcohol=1 if strpos(tekst,"vinmonopol")
replace alcohol=1 if strpos(tekst,"brændevin")

gen socialwelfare=0
replace socialwelfare=1 if strpos(tekst,"forsikring")
replace socialwelfare=1 if strpos(tekst,"trygd")
replace socialwelfare=1 if strpos(tekst,"pension")

gen constitution=0
replace constitution=1 if strpos(tekst,"grundlov")
replace constitution=1 if strpos(tekst,"grunnlov")
replace constitution=1 if strpos(tekst,"gr.lov")

gen budget=1
replace budget=0 if budsjet=="NULL"


************* DROPPING THE ACTUAL VOTES ON THE ELECTORAL REFORM *******************
drop if votnr_new==-130
drop if votnr_new==-129
drop if votnr_new==-128
drop if votnr_new==-127
drop if votnr_new==-126

*leaving us with 36,622 MP-vote observations. 
di 63617-15107-8262-205-2876-545


/*
Sal (SAL)
Koder:
1=Stortinget i plenum  /* 70% */
2=Odelsting            /* 26% */
3=Lagting              /* 4% */
Voteringsnummer (VOTNR)
Voteringsnummer innenfor Sesjon og Sal.
*/

gen storting=0
replace storting=1 if sal==1

gen odelsting=0
replace odelsting=1 if sal==2

gen PRimplement=votingyear>1921
gen PRadopt=votingyear>1919  /* this is not exactly accurate since adopt was Dec 1919  , NEED TO FIX */

tab pid_NSD, gen(zz)
egen yy=mean(PRimplement), by(pid_NSD)

reg deviation PRimplement odelsting zz* if yy!=0 & yy!=1, cluster(lpnr)
reg deviation PR* odelsting zz* if yy!=0 & yy!=1, cluster(lpnr)
sum deviation if PRimplement==0
sum deviation if PRimplement==1

gen Reform=votnr_new>=0

gen KnudsenCabinet=0
replace KnudsenCabinet=1 if votnr_new<-96.5 & sal==1  /* Knudsen cabinet */
replace KnudsenCabinet=1 if votnr_new<-77.5 & sal==2 /* Knudsen cabinet */

gen Halvorsen1Cabinet=0
replace Halvorsen1Cabinet=1 if votnr_new>-96.5 & votnr_new<-29.5 & sal==1  /* Halvorsen 1 cabinet */
replace Halvorsen1Cabinet=1 if votnr_new>-77.5 & votnr_new<-18.5 & sal==2  /* Halvorsen 1 cabinet */

gen BlehrCabinet=0
replace BlehrCabinet=1 if votnr_new>-29.5 & votnr_new<50.5 & sal==1  /* Blehr cabinet */
replace BlehrCabinet=1 if votnr_new>-18.5 & votnr_new<15.5 & sal==2  /* Blehr cabinet */

gen Halvorsen2Cabinet=0  /* also include Berge */
replace Halvorsen2Cabinet=1 if votnr_new>50.5 & sal==1  /* Halvorsen 2 cabinet */
replace Halvorsen2Cabinet=1 if votnr_new>15.5 & sal==2  /* Halvorsen 2 cabinet */

save dta/Voteringsarkivet, replace
restore

**********************************************************************************************
******************************************** PREPARE FILE FOR FIGURE A.1 *********************
**********************************************************************************************
sort pid_NSD electionperiod
merge pid_NSD electionperiod using dta\DataSet_Elected1906_1936

*drop if votingyear>1923
*drop if votingyear<1920
drop if votingyear<1910 /* party system in flux before 1909 election */
tab _merge

*** number of unique votes
preserve
gen count=1
collapse count sal, by(lpnr)
li
li if sal!=3
restore

drop if sal==3  /* DROP LAGTING */
sum

egen min_votnr_new=min(votnr_new), by(pid_NSD)  /* first vote MP is casting */
egen max_votnr_new=max(votnr_new), by(pid_NSD)  /* last vote MP is casting */
*tab candidatename_ed if max_votnr==-1  /* candidates exiting before PR */
*tab candidatename_ed if min_votnr==0  /* candidates entering with PR */

*****assert _merge!=2  /* no observations from DataSet only */
drop if _merge==2

tab _merge
drop if _merge==1  /* only keep observations where we identify candidate */

***************************************************************************************************************
*********** for each vote (identified by votnr) find voting cohesion by party**********************************
***************************************************************************************************************
** Votering (VOT)
*0=Møtte ikke, permitert
*1=Stemte i flertallet
*2=Stemte i mindretallet
*3=Var fraværende i voteringen
*NULL= missing

gen vote=0
replace vote=1 if vot=="1"
replace vote=2 if vot=="2"
*replace vote=3 if vot=="3"  /* updated */

gen missing=0
replace missing=1 if vote==0
drop if missing==1 /* MP not present */

gen partybloc="."
replace partybloc="Labor" if party=="s"|party=="dna"|party=="nsa" 
replace partybloc="Conservative" if party=="h"|party=="h_fv"|party=="fv"
replace partybloc="Liberal" if party=="v"|party=="v_a"|party=="a"|party=="rf"
tab party if partybloc=="."
tab partyname if partybloc=="."

egen partybloc_missing=mean(missing), by(lpnr partybloc)
*hist partybloc_missing

egen case_missing=mean(missing), by(lpnr)
*hist partybloc_missing

drop if missing==1 /* MP permitert */
tab party if partybloc=="."  
keep if partybloc!="."  /* independent and farmers party */

*******************************************************************************
*******************************************************************************
*******************************************************************************
gen count=1
gen count_with_majority=0
replace count_with_majority=1 if vote==1
replace count_with_majority=. if vote==3 /* MP abstained */

gen count_with_minority=0
replace count_with_minority=1 if vote==2
replace count_with_minority=. if vote==3 /* MP abstained */

egen partybloc_with_majority=mean(count_with_majority), by(lpnr partybloc)
egen overall_with_majority=mean(count_with_majority), by(lpnr)

egen partybloc_with_minority=mean(count_with_minority), by(lpnr partybloc)

gen deviation=0
replace deviation=1 if partybloc_with_majority<0.5 & vote==1 /*candidate voting with majority, partybloc not voting with majority */
replace deviation=1 if partybloc_with_majority>0.5 & vote==2 /*candidate not voting with majority, partybloc voting with majority */
*******replace deviation=1 if vote==3 /*candidate abstaining --- updated */

*******************************************************************************
*******************************************************************************
*******************************************************************************
gen vote_=vote
collapse deviation partybloc_missing votingyear (mean) vote (sd) vote_ (sum) count (sum) count_with_majority (sum) count_with_minority, by(lpnr partybloc)

gen rice=abs(count_with_majority-count_with_minority)/count

rename vote_ sd_vote
rename vote mean_vote
rename count number_of_partybloc_votes
bysort votingyear: tab partybloc

gen count=1
collapse deviation sd_vote rice (sum) count, by(votingyear partybloc)

save dta/Voteringsarkivet_Yearly, replace

erase "dta/GenerateVotenumber_1_.dta"
erase "dta/GenerateVotenumber_2_.dta"
erase "dta/GenerateVotenumber_3_.dta"
